package com.wg.dao;

import com.wg.entity.User;
import com.wg.model.DBOperator;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class UserDao {
    public List<User> users;

    private UserDao(){
        users = findAll();
    }

    private static UserDao userDao;

    public static UserDao getInstance(){
        return SingletonHander.singleton;
    }

    private static class SingletonHander{
        private static final UserDao singleton = new UserDao();
    }


    /**
     * 功能描述:查询所有数据
     */
    public List<User> findAll() {
        List<User> ret = new ArrayList<>();
        String pathName = ".\\src\\main\\resources\\saves\\User.data";

        try(FileReader reader = new FileReader(pathName);
            BufferedReader br = new BufferedReader(reader)){
            String line;
//           按行读取数据
            while ((line = br.readLine()) != null && line.isEmpty() == false){
                User user = new User();
                user.deserialize(line);
                ret.add(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ret;
    }
    /**
     * 功能描述: 由userId查询数据
     */
    public User findById(Integer userId) {
        List<User> ret = users.stream().filter(user -> user.id == userId).collect(Collectors.toList());
        return ret.get(0);
    }
    /**
     * 功能描述: 添加数据
     */
    public boolean addUser(User user) {
        users.add(user);
        return update();
    }

    /**
     * 功能描述: 由userId删除数据
     */
    public boolean deleteById(Integer userId) {
        users.removeIf(user -> user.id == userId);
        return update();
    }
    /**
     * 功能描述:由userId修改数据
     */
    public boolean updateById(User user) {
        for (User u: users) {
            if(u.id == user.id){
                u = user;
            }
        }
        return update();
    }
    /**
     * 功能描述:通过判断账号密码正确与否进行查询
     * @return
     */
    public User queryUserByNameAndPwd(User user) {
        System.out.println("user:" + user.serialize());
//        List<User> ret = users.stream().filter(u -> u.name == user.name && u.password == user.password).collect(Collectors.toList());
        List<User> ret = users.stream().filter(u -> {
            System.out.println("flag:" + (u.name.equals(user.name) && u.password.equals(user.password)));
            return u.name.equals(user.name) && u.password.equals(user.password);
        }).collect(Collectors.toList());
        System.out.println("ret:"+ret);
        if(ret.size() != 0) {
            return ret.get(0);
        }
        return null;
    }

    /**
     * 功能描述:把数据更新到数据库
     */
    public boolean update() {
        String pathName = ".\\src\\main\\resources\\saves\\User.data";

        try {
            File writeName = new File(pathName);
            writeName.createNewFile();
            try (FileWriter writer = new FileWriter(writeName, true);
            BufferedWriter out = new BufferedWriter(writer)){
                for (User u : users) {
                    out.write(u.serialize());
                    out.newLine();   // 换行
                }
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
